﻿var CurrentRole={};

Ext.onReady(function () {
    PageSafe();

    var rolestore = new Ext.data.JsonStore({
        url: Ext.getClassUrl('BaseManager.RoleView', 'GetRoles'),
        root: 'ResponseObject',
        fields: ['PID', 'RoleName', 'Creater', 'LastUpdate', 'NTime', 'CreateName.UserName'],
        listeners: {
            beforeload: function (st, op) {
                var re = new JsonRequest();
                re.category = CATEGORY.READONLY;
                st.baseParams = re;
                st.proxy.conn.jsonData = re;
            }
        }
    });
    var rolesm = new Ext.grid.CheckboxSelectionModel({
        singleSelect: true,
        listeners: {
            rowselect: function () {
                var k = rolegrid.getSelectionModel().selections.items;
                if (k.length > 0) {
                    CurrentRole = k[0].json;
                }
            },
            rowdeselect: function () {
                CurrentRole = {};
            }
        }
    });
    var rolegrid = new Ext.grid.GridPanel({
        bodyStyle: 'padding:2px',
        border: false,
        split: true,
        height: 500,
        sm: rolesm,
        loadMask: true, //加载
        columns: [
       rolesm,
       { id: 'RoleName', header: '角色名', width: 100, dataIndex: 'RoleName', sortable: true },
       { id: 'CreateName.UserName', header: '发布人', width: 100, dataIndex: 'CreateName.UserName', sortable: true },
       { id: 'NTime', header: '发布时间', width: 200, dataIndex: 'NTime', sortable: true }
   ],
        store: rolestore
    });

    var page = new Ext.Viewport({
        layout: 'border',
        items: [{
            region: 'center',
            title: '角色管理',
            iconCls: 'role-icon',
            tbar: [{ text: '增加角色', iconCls: 'add-icon', handler: function () {
                var addwin = new Ext.Window({
                    width: 300,
                    resizable: false,
                    modal: true,
                    height: 120,
                    title: '角色新增',
                    iconCls: 'role-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '角色名',
                        width: 100,
                        id: 'RoleName',
                        name: 'RoleName'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        var re = new JsonRequest();
                        re.params.info = {
                            RoleName: Ext.getCmp("RoleName").getValue()
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.RoleView', 'AddRole'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("角色管理提示", "角色已经增加成功", function () { rolestore.load(); addwin.close(); });
                                }
                                else {
                                    Ext.Msg.alert("角色管理提示", "角色增加失败，请联系管理员", function () { rolestore.load(); addwin.close(); });
                                }
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { addwin.close(); } }]
                }).show();
            }
            }, { text: '修改角色', iconCls: 'update-icon', handler: function () {
                if (CurrentRole.PID == null) { Ext.Msg.alert("提示信息", "请选择需要修改的角色"); return; }
                var upwin = new Ext.Window({
                    width: 300,
                    resizable: false,
                    modal: true,
                    height: 120,
                    title: '角色新增',
                    iconCls: 'role-icon',
                    layout: 'form',
                    bodyStyle: 'padding: 10px',
                    defaults: { xtype: 'textfield', style: 'margion-top:20px' },
                    labelAlign: 'right',
                    items: [{
                        fieldLabel: '角色名',
                        width: 100,
                        id: 'RoleName',
                        name: 'RoleName'
                    }],
                    buttons: [{ text: '保存', iconCls: 'save-icon', handler: function () {
                        var re = new JsonRequest();
                        re.params.info = {
                            PID: CurrentRole.PID,
                            RoleName: Ext.getCmp("RoleName").getValue(),
                            LastUpdate: CurrentRole.LastUpdate,
                            Creater: CurrentRole.Creater
                        }
                        Ext.Ajax.request({
                            url: Ext.getClassUrl('BaseManager.RoleView', 'UpdateRole'),
                            jsonData: re,
                            success: function (re, op) {
                                var obj = Ext.decode(re.responseText);
                                if (obj.ResponseObject != null) {
                                    Ext.Msg.alert("角色管理提示", "角色已经修改成功", function () { rolestore.load(); upwin.close(); });
                                }
                                else {
                                    Ext.Msg.alert("角色管理提示", "角色修改失败，请联系管理员", function () { rolestore.load(); upwin.close(); });
                                }
                            }
                        });
                    }
                    }, { text: '取消', iconCls: 'cancel-icon', handler: function () { upwin.close(); } }],
                    listeners: {
                        render: function () {
                            if (CurrentRole.PID != null) {
                                Ext.getCmp("RoleName").setValue(CurrentRole.RoleName);
                            }
                        }
                    }
                }).show();
            }
            }, { text: '删除角色', iconCls: 'del-icon', handler: function () {
                if (CurrentRole.PID == null) { Ext.Msg.alert("提示信息", "请选择需要删除的角色"); return; }
                var re = new JsonRequest();
                re.params.info = CurrentRole;
                Ext.Ajax.request({
                    url: Ext.getClassUrl('BaseManager.RoleView', 'DelRole'),
                    jsonData: re,
                    success: function (re, op) {
                        var obj = Ext.decode(re.responseText);
                        if (obj.ResponseObject != null) {
                            Ext.Msg.alert("角色管理提示", "角色已经删除成功", function () { rolestore.load();});
                        }
                        else {
                            Ext.Msg.alert("角色管理提示", "角色删除失败，请联系管理员", function () { rolestore.load(); });
                        }
                    }
                });
            } 
            }],
            items: rolegrid
        }],
        listeners: {
            render: function () {
                rolestore.load();
            }
        }
    });
});